#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import rospy
import json
from flask import Blueprint,request,Response

from parser.hardware_parser import Hardware_Parser
from tool.common import *

from tool import Log_Parser
log = Log_Parser()

hardware_parser = Hardware_Parser()
hardware_server = Blueprint("/hardware",__name__)

#---------------------------------#
#******#   flask-网络端口验证   #******#
#---------------------------------#
@hardware_server.before_request
def before():
    res_data = before_user(request)
    if res_data:
        return Response(json.dumps(res_data),  mimetype='application/json')


#---------------------------------#
    #******#   POST   #******#
#---------------------------------#
# 获取传感器信息列表
# req: 
# res: 
@hardware_server.route('/sensor_list',methods=['POST'])
def Post_GetSensorList():
    res_data = hardware_parser.GetSensorList()
    return Response(json.dumps(res_data),  mimetype='application/json')

#---------------------------------#
    #******#   POST   #******#
#---------------------------------#
# 设置传感器使能检测
# req: 
# res: 
@hardware_server.route('/sensor_enable',methods=['POST'])
def Post_SetSensorEnabled():
    data = request.json
    res_data = hardware_parser.SetSensorEnabled(data)
    return Response(json.dumps(res_data),  mimetype='application/json')



#---------------------------------#
    #******#   POST   #******#
#---------------------------------#
# 手动模式设置
# req: 
# res: 
@hardware_server.route('/hand_mode',methods=['POST'])
def Post_SetHandMode():
    data = request.json
    if data:
        mode = "手动" if data["mode"] == 1 else "自动"
        log.LogInfo(rospy.get_name(), "request_info: {}, 请求切换为 {} 控制模式".format(getRequestInfo(request), mode))
    res_data = hardware_parser.SetHandMode(data)
    return Response(json.dumps(res_data),  mimetype='application/json')


#---------------------------------#
    #******#   POST   #******#
#---------------------------------#
# 充电开关
# req: 
# res: 
@hardware_server.route('/electrode_switch',methods=['POST'])
def Post_SetChargeSwitch():
    data = request.json
    res_data = hardware_parser.SetChargeSwitch(data)
    return Response(json.dumps(res_data),  mimetype='application/json')

#---------------------------------#
# ft.wang 20240606接手
#---------------------------------#
# 获取外设列表
@hardware_server.route('/utils_list',methods=['POST'])
def Post_GetDeviceList():
    res_data = hardware_parser.GetDeviceList()
    return Response(json.dumps(res_data),  mimetype='application/json')

# 外设设置
@hardware_server.route('/utils_ctrl',methods=['POST'])
def Post_SetDeviceState():
    data = request.json
    res_data = hardware_parser.SetDeviceState(data)
    return Response(json.dumps(res_data),  mimetype='application/json')

# 外设控制模式
@hardware_server.route('/utils_ctrl_mode',methods=['POST'])
def Post_SetDevicesdMode():
    data = request.json
    res_data = hardware_parser.SetDevicesdMode(data)
    return Response(json.dumps(res_data),  mimetype='application/json')